import java.io.*;
import java.util.HashMap;
import java.util.Map;

/*
ID: qingtia2
LANG: JAVA
TASK: gift1
*/
class gift1 {

    public static void main(String[] args) throws Exception{
        BufferedReader in = new BufferedReader(new FileReader("gift1.in"));
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("gift1.out")));
        int personNum = Integer.parseInt(in.readLine());
        Map<String, Integer> map = new HashMap<>();
        String[] names = new String[personNum];
        for (int i = 0; i < personNum; i++) {
            String name = in.readLine();
            map.put(name, 0);
            names[i] = name;
        }
        String name;
        while (null != (name = in.readLine())) {
            Integer integer = map.get(name);
            String[] strs = in.readLine().split(" ");
            if (Integer.parseInt(strs[1]) == 0) {
                map.put(name, integer + Integer.parseInt(strs[0]));
            } else {
                Integer div = Integer.parseInt(strs[0]) / Integer.parseInt(strs[1]);
                Integer mod = Integer.parseInt(strs[0]) % Integer.parseInt(strs[1]);
                integer = integer + mod - Integer.parseInt(strs[0]);
                map.put(name, integer);
                for (int j = 0; j < Integer.parseInt(strs[1]); j++) {
                    String otherName = in.readLine();
                    Integer other = map.get(otherName) + div;
                    map.put(otherName, other);
                }
            }
        }
        for (int i = 0; i < names.length; i++) {
            out.println(names[i] + " " + map.get(names[i]));
        }
        out.close();
    }
}
